package com.hspedu.qqclient.view;

import com.hspedu.qqclient.service.UserClientService;
import com.hspedu.qqclient.utils.Utility;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/16
 * @time 23:48
 * 客户端的菜单界面
 */
@SuppressWarnings("all")
public class QQView {

    private boolean loop = true;
    private String key;
    private UserClientService userClientService = new UserClientService();

    /**
     * 显示主菜单
     */
    private void mainMenu() {


        System.out.println("===========欢迎登录网络通信系统===========");
        System.out.println("\t\t 1 登录系统");
        System.out.println("\t\t 9 退出系统");

        while (loop) {
            System.out.print("请输入你的选择: ");
            key = Utility.readString(1);

            //根据用户的输入，来处理不同的逻辑
            switch (key) {
                case "1":
                    System.out.print("请输入用户号：");
                    String userId = Utility.readString(50);
                    System.out.print("请输入密  码：");
                    String password = Utility.readString(50);
                    //需要构建User对象并发送到服务器验证
                    if (userClientService.checkUser(userId, password)) {
                        while (loop) {
                            System.out.println("\n=========网络通信系统二级菜单(用户 " + userId + " )=======");
                            System.out.println("\t\t 1 显示在线用户列表");
                            System.out.println("\t\t 2 群发消息");
                            System.out.println("\t\t 3 私聊消息");
                            System.out.println("\t\t 4 发送文件");
                            System.out.println("\t\t 9 退出系统");
                            System.out.print("请输入你的选择: ");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    userClientService.getOnlineFriendList();
                                    break;
                                case "2": {
                                    System.out.println("请输入想说的话:");
                                    String content = Utility.readString(50);
                                    userClientService.groupChat(content);
                                    break;
                                }
                                case "3": {
                                    System.out.println("请输入想聊天的用户(在线)");
                                    String targetUserName = Utility.readString(50);
                                    System.out.println("请输入想说的话:");
                                    String content = Utility.readString(50);
                                    userClientService.privateChat(targetUserName, content);
                                    break;
                                }
                                case "4":
                                    System.out.println("请输入想发送的用户(在线)");
                                    String targetUser = Utility.readString(50);
                                    System.out.println("请输入像要发送的文件的完整路径：");
                                    String srcFilePath = Utility.readString(100);
                                    System.out.println("请输入发送文件到对方的路径：");
                                    String destFilePath = Utility.readString(100);
                                    userClientService.transferBinaryFile(targetUser,srcFilePath,destFilePath);
                                    break;
                                case "9":
                                    /**
                                     * 必须结束和服务器正在通信的子线程
                                     * 把loop置为false只是单独结束main线程
                                     * 此时还有子线程在运行
                                     * 整个进程也不会结束
                                     * 单方面在这里调用System.exit(0)也可以推出客户端程序
                                     * 但是由于没有通知服务器端，服务器端会一直抛出各种异常
                                     * 因此客户端如果需要退出，也应该退出前通知服务器
                                     */
                                    loop = false;
                                    userClientService.clientRequestExit();
                                    break;
                            }
                        }

                    } else {
                        System.out.println("登陆失败");
                    }
                    break;
                case "9":
                    loop = false;
                    break;
                default:
                    System.out.println("输入非法，请重新输入");
                    break;
            }

        }
        System.out.println("QQMenu任务结束");
    }

    public static void main(String[] args) {
        new QQView().mainMenu();
    }

}
